import pandas as pd

sheet = '主表'


df = pd.read_excel(io=rf'', sheet_name= sheet, index_col='项目名称（勿修改）',header=1)

gf = df.groupby('市州')
ztz = (gf[['总投资','剩余投资','年度投资（万）']].sum()/10000)
l = gf.size().rename('计数')



print(l)
df_new7 = pd.concat([ztz, l],axis=1,join='inner')



df_new1 = pd.DataFrame(ztz)
df_new2 = pd.DataFrame(ztz.sum())
gf = df.groupby('领域一级')
ztz = (gf[['总投资','剩余投资','年度投资（万）']].sum()/10000)
df_new3 = pd.DataFrame(ztz)
gf = df.groupby(['领域一级','领域二级'])
ztz = (gf[['总投资','剩余投资','年度投资（万）']].sum()/10000)
df_new4 = pd.DataFrame(ztz)
gf =df.groupby(['市州','领域一级'])
ztz = (gf[['总投资','剩余投资','年度投资（万）']].sum()/10000)
df_new5 = pd.DataFrame(ztz)

gf =df.groupby(['市州','区县'])
ztz = (gf[['总投资','剩余投资','年度投资（万）']].sum()/10000)
df_new6 = pd.DataFrame(ztz)


df_new1.to_excel(r'D:\Desktop\统计\市州汇总.xlsx',sheet_name='市州汇总')
df_new2.to_excel(r'D:\Desktop\统计\汇总.xlsx',sheet_name='汇总')
df_new3.to_excel(r'D:\Desktop\统计\领域一级汇总.xlsx',sheet_name='领域一级汇总')
df_new4.to_excel(r'D:\Desktop\统计\领域二级汇总.xlsx',sheet_name='领域二级汇总')

df_new5.to_excel(r'D:\Desktop\统计\市州领域一级汇总.xlsx',sheet_name='市州领域一级汇总')

df_new6.to_excel(r'D:\Desktop\统计\市州区县汇总.xlsx',sheet_name='市州区县汇总')
df_new7.to_excel(r'D:\Desktop\统计\1.xlsx',sheet_name='1')

import pandas as pd

# 示例数据
data = {
    'Category': ['A', 'B', 'A', 'B', 'A', 'B', 'A'],
    'Value': [10, 20, 15, 25, 5, 30, 10]
}
df = pd.DataFrame(data)

# 计算每个分组内Value的占比
df['Value_Share'] = df.groupby('Category')['Value'].transform(lambda x: x / x.sum())

print(df)






